<?php

/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 */

/**
 * Class with methods to organise Camelot XML Packets
 *
 * @author Bendsoft
 */
class CamelotXmlSorting {

    /**
     * Method to generate the sorting array of the list schema
     *
     * @access public
     * @param string $schema XML formatted SharePoint Schema
     * @param array $columns Which columns to use, if not set use all
     * @return object with sorting structure
     */
    public function sort_schema($schema, $columns = null) {

        if (!$schema) {
            return new CamelotException(null, 502);
        }
        
        // Load this object space if available
        if (isset($columns)) {
            $this->_columns = $columns;
        }

        // Convert the xml-based schema to SimpleXmlObject
        $schema = simplexml_load_string($schema);

        // Sorting array
        $sort = array();
        $i = 0;

        foreach ($schema as $key => $value) {

            // Retrieves the list name
            $schema_attribute_name = $this->xml_attribute($value, 'Name');

            // Get the friendly version of the display name
            $schema_display_name = (string) $value->DisplayName;

            // Validate that the display name has a value, if not we set the attribute name
            $schema_display_name = ($schema_display_name == "") ? $schema_attribute_name : $schema_display_name;

            // Set the datatypes
            $schema_data_type = ($value->Type == "") ? (string) $value->DataType : (string) $value->Type;


            // If specific columns are stated
            if (isset($this->_columns) && is_array($this->_columns)) {

                // If the column is in the array
                if (in_array($schema_attribute_name, $this->_columns)) {

                    $this->initiate_sorted();

                    // Add data to sorting array
                    $this->_sorted->_schema->attribute_name->$i = $schema_attribute_name;
                    $this->_sorted->_schema->display_name->$i = $schema_display_name;
                    $this->_sorted->_schema->data_type->$i = $schema_data_type;
                    $i++;
                }

                // If no columns are specified (use all)
            } else {
                $this->initiate_sorted();

                // Add data to sorting array
                $this->_sorted->_schema->attribute_name->$i = $schema_attribute_name;
                $this->_sorted->_schema->display_name->$i = $schema_display_name;
                $this->_sorted->_schema->data_type->$i = $schema_data_type;
                $i++;
            }
        }

        // Cast the sorted schema
        $this->_sorted->_schema->attribute_name = (array) $this->_sorted->_schema->attribute_name;
        $this->_sorted->_schema->display_name = (array) $this->_sorted->_schema->display_name;

        return $this->_sorted->_schema;
    }

    /**
     * Initiate the sorting array to not trigger any strict checks
     */
    private function initiate_sorted() {
        if (!isset($this->_sorted)) {
            $this->_sorted = new StdClass();
        }
        if (!isset($this->_sorted->_schema)) {
            $this->_sorted->_schema = new StdClass();
        }
        if (!isset($this->_sorted->_schema->attribute_name)) {
            $this->_sorted->_schema->attribute_name = new StdClass();
        }
        if (!isset($this->_sorted->_schema->display_name)) {
            $this->_sorted->_schema->display_name = new StdClass();
        }
        if (!isset($this->_sorted->_schema->data_type)) {
            $this->_sorted->_schema->data_type = new StdClass();
        }
    }

    /**
     * Creates the same structure of the content as the list schema
     * This is not default in SharePoint
     *
     * @access public
     * @param the xml formatted content
     * @param sortingarray from the function sortSchema
     * @return array
     */
    public function sort_content($content, $sortingarray) {

        if (!$content) {
            return new CamelotException(null, 500);
        }
        if (!$sortingarray) {
            return new CamelotException(null, 501);
        }

        // Create usable array from the content data
        $innerdata = array();
        foreach ($content as $key => $value) {
            /**/
            $listxml = (array) simplexml_load_string($value);
            array_shift($listxml); // Remove the attributes, we dont use them here / [@attributes] => Array ( [Index] => 0 )
            $innerdata[] = $listxml;
            $listxml = null;
        }

        $left = $sortingarray;
        $right = $innerdata;

        // Match the list array structure with the sorting array (the schema)
        $list = array();
        foreach ($right as $array) {
            foreach ($left as $field) {
                $temp[$field] = $array[$field];
            }
            $list[] = $temp;
        }

        $this->_sorted->_content = $list;
        return $this->_sorted->_content;
    }

    /**
     * Convert xml attributes
     *
     * @access private
     * @param object $object
     * @param string $attribute
     * @return string
     */
    private function xml_attribute($object, $attribute) {
        if (isset($object[$attribute]))
            return (string) $object[$attribute];
    }

}

?>
